<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="reportList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="设备名称" align="center" prop="year" />-->
      <el-table-column label="餐次日期" align="center" prop="time" />
      <el-table-column label="餐次" align="center" prop="number" />
      <el-table-column label="试吃时间" align="center" prop="time1" />
      <el-table-column label="试吃人" align="center" prop="person" />
      <el-table-column label="试吃情况" align="center" prop="person1" />
      <el-table-column label="留样时间" align="center" prop="time2" />
      <el-table-column label="留样人" align="center" prop="person2" />
      <el-table-column label="销样时间" align="center" prop="time3" />
      <el-table-column label="销样人" align="center" prop="person3" />
      <el-table-column label="检查人" align="center" prop="person4" />
      <!-- <el-table-column label="占位" align="center" prop="visionResult">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.vision_result" :value="scope.row.visionResult" />
          </template>
        </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.readTime == null"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >详情</el-button
          >
          <!-- <el-button
            v-if="scope.row.readTime == null"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作汇报对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="餐次日期" prop="time">
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择餐次时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="餐次" prop="number">
          <el-select
            v-model="form.number"
            clearable
            filterable
            placeholder="请选择餐次"
          >
            <el-option
              v-for="item in numberList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="small"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="试吃时间" prop="time1">
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="form.time1"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择试吃时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="试吃人" prop="person">
          <el-input v-model="form.person" placeholder="请填写试吃人"></el-input>
        </el-form-item>
        <el-form-item label="试吃情况" prop="person1">
          <el-input
            v-model="form.person1"
            placeholder="请填写试吃情况"
          ></el-input>
        </el-form-item>
        <el-form-item label="留样时间" prop="time2">
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="form.time2"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择留样时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="留样人" prop="person2">
          <el-input
            v-model="form.person2"
            placeholder="请填写留样人"
          ></el-input>
        </el-form-item>
        <el-form-item label="销样时间" prop="time3">
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="form.time3"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择销样时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="销样人" prop="person3">
          <el-input
            v-model="form.person3"
            placeholder="请填写销样人"
          ></el-input>
        </el-form-item>
        <el-form-item label="检查人" prop="person4">
          <el-input
            v-model="form.person4"
            placeholder="请填写检查人"
          ></el-input>
        </el-form-item>
        <el-form-item v-if="form.image == ''" label="菜肴图片" prop="image">
          <ImageUpload @input="getImageList" />
        </el-form-item>
        <el-form-item v-if="form.image !== ''" label="菜肴图片" prop="image">
          <img
            src="@/assets/image/baozi.png"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import { selectClazz } from "@/api/base/clazz";
import {
  addstuVision,
  delstuVision,
  stuVision,
  updatestuVision,
} from "@/api/base/ill";
import {
  addStudent,
  delStudent,
  listStudent,
  updateStudent,
} from "@/api/base/student";
export default {
  name: "gzt",
  dicts: ["vision_result", "year_type"],
  data() {
    const leftEye = (rule, value, callback) => {
      if (value > 2) {
        callback(new Error("左眼度数最高为2.0"));
      } else {
        callback();
      }
    };
    const rightEye = (rule, value, callback) => {
      if (value > 2) {
        callback(new Error("右度数最高为2.0"));
      } else {
        callback();
      }
    };
    return {
      title: "",
      open: false,
      form: {},
      rules: {
        grade: [{ required: true, message: "不能为空", trigger: "blur" }],
        classId: [{ required: true, message: "不能为空", trigger: "blur" }],
        stuId: [{ required: true, message: "不能为空", trigger: "blur" }],
        visionResult: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        leftEyeVision: [
          { required: true, message: "不能为空", trigger: "blur" },
          { required: true, validator: leftEye, trigger: "blur" },
        ],
        rightEyeVision: [
          { required: true, message: "不能为空", trigger: "blur" },
          { required: true, validator: rightEye, trigger: "blur" },
        ],
      },
      // 总条数
      total: 0,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolCode: this.$store.getters.schoolCode,
      },
      classList: [],
      reportList: [],
      stuList: [],
      numberList: [
        {
          value: "1",
          label: "早",
        },
        {
          value: "2",
          label: "午",
        },
        {
          value: "3",
          label: "下午茶",
        },
      ],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
    };
  },
  created() {
    /**列表接口**/
    this.getList();
  },
  methods: {
    // 获取上传后的图片
    getImageList(val) {
      this.form.image = val;
    },
    /** 查询视力列表 */
    async getList() {
      try {
        this.loading = true;
        const data = await stuVision(this.queryParams);
        this.reportList = [
          {
            time: "2024-01-30",
            number: "1",
            time1: "2024-01-30 00:08:00",
            person: "张小明",
            person1: "味道不错",
            time2: "2024-01-30 00:08:10",
            person2: "张小三",
            time3: "2024-01-30 00:10:00",
            person3: "张小花",
            person4: "张萌萌",
            image: "",
          },
        ];
        this.total = 0;
        this.loading = false;
      } catch (e) {
        console.log(e);
      }
    },
    /** 查询条件班级列表 */
    async getFormclassList() {
      try {
        let params = {};
        this.$set(this.queryParams, "classId", "");
        if (this.queryParams.grade) params.year = this.queryParams.grade;
        const { rows } = await selectClazz(params);
        this.classList = rows.map((item) => {
          return {
            id: item.id,
            label: item.title,
          };
        });
      } catch (e) {
        console.log(e);
      }
    },
    /** 查询班级列表 */
    async getclassList() {
      try {
        let params = {};
        this.$set(this.form, "classId", "");
        this.$set(this.form, "stuId", "");
        if (this.form.grade) params.year = this.form.grade;
        const { rows } = await selectClazz(params);
        this.classList = rows.map((item) => {
          return {
            id: item.id,
            label: item.title,
          };
        });
      } catch (e) {
        console.log(e);
      }
    },
    /**查询学生**/
    async getStuList() {
      try {
        let params = {};
        this.$set(this.form, "stuId", "");
        if (this.form.classId) params.classId = this.form.classId;
        const data = await listStudent(params);
        this.stuList = data.rows;
      } catch (e) {
        console.log(e);
      }
    },
    /**提交*/
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //重新组装设备信息

          if (this.form.id != null) {
            updatestuVision(this.form).then((response) => {
              if (response.code == 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addstuVision(this.form).then((response) => {
              if (response.code == 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      //   this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      //   const id = row.id || this.ids;
      this.form = row;
      this.form.image = "@/assets/image/baozi.png";
      console.log(this.form);
      //   this.form = JSON.parse(JSON.stringify(row));
      /**班级**/
      //   this.getclassList();
      /**学生接口**/
      //   this.getStuList();
      this.open = true;
      this.title = "查看食品留样记录详情";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加食品留样记录";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      // this.$confirm('是否确认删除学生编号为"' + ids + '"的数据项?', "警告", {
      this.$confirm("是否确认删除选中的学生数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delstuVision(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stuId: null,
        leftEyeVision: null,
        rightEyeVision: null,
        visionResult: null,
        classId: null,
        image: "",
        schoolCode: this.$store.getters.schoolCode,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
  